{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-10T00:53:56.889669Z",
     "start_time": "2025-03-10T00:53:54.982542Z"
    }
   },
   "source": [
    "from nltk.translate.bleu_score import sentence_bleu\n",
    "\n",
    "# prefect match，reference是target,candidate是预测输出的\n",
    "reference = [['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']]\n",
    "candidate = ['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']\n",
    "score = sentence_bleu(reference, candidate)\n",
    "print(score)\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-10T00:54:08.245403Z",
     "start_time": "2025-03-10T00:54:08.241895Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# one word different,默认是4-gram\n",
    "candidate = ['the', 'fast', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']\n",
    "# score = sentence_bleu(reference, candidate,weights=(0.25, 0.25, 0.25, 0.25))\n",
    "score = sentence_bleu(reference, candidate, weights=(1, 0, 0, 0))\n",
    "print(score)"
   ],
   "id": "f1e92b7de8b8a90b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8888888888888888\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-10T00:54:34.167834Z",
     "start_time": "2025-03-10T00:54:34.163479Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "np.exp(np.log(8 / 9))"
   ],
   "id": "704cb7d7120b78b7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8888888888888888"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-10T00:54:35.280365Z",
     "start_time": "2025-03-10T00:54:35.275893Z"
    }
   },
   "cell_type": "code",
   "source": "np.exp(1 / 4 * np.log(8 / 9) + 1 / 4 * np.log(6 / 8) + 1 / 4 * np.log(5 / 7) + 1 / 4 * np.log(4 / 6))",
   "id": "2054c32429c2f6d7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7506238537503395"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-10T00:54:43.355946Z",
     "start_time": "2025-03-10T00:54:43.351931Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# two words different\n",
    "\n",
    "candidate = ['the', 'fast', 'brown', 'fox', 'jumped', 'over', 'the', 'sleepy', 'dog']\n",
    "score = sentence_bleu(reference, candidate)\n",
    "print(score)"
   ],
   "id": "eaf0cb2e724b2674",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.4854917717073234\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-10T00:54:48.908311Z",
     "start_time": "2025-03-10T00:54:48.904713Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除candidate句子最后两个单词, 即让candidate变短，缺失词需要考虑精确度\n",
    "\n",
    "candidate = ['the', 'fast', 'brown', 'fox', 'jumped', 'over', 'the']\n",
    "score = sentence_bleu(reference, candidate)\n",
    "print(score)"
   ],
   "id": "9fb8496e5a524f77",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.4835447404743731\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-10T00:54:55.932282Z",
     "start_time": "2025-03-10T00:54:55.927940Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "np.exp(1 / 4 * np.log(5 / 7) + 1 / 4 * np.log(4 / 6) + 1 / 4 * np.log(3 / 5) + 1 / 4 * np.log(2 / 4)) * np.exp(-2 / 9)"
   ],
   "id": "3d0df89cc3f1eb83",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.49228386893821674"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-10T00:55:00.981006Z",
     "start_time": "2025-03-10T00:55:00.975508Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 4-gram cumulative BLEU\n",
    "from nltk.translate.bleu_score import sentence_bleu\n",
    "\n",
    "reference = [['this', 'is', 'small', 'test']]\n",
    "candidate = ['this', 'is', 'a', 'test']\n",
    "score = sentence_bleu(reference, candidate)\n",
    "print(score)\n",
    "score = sentence_bleu(reference, candidate, weights=(1, 0, 0, 0))\n",
    "score\n"
   ],
   "id": "d43d2c0eb89106bf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0547686614863434e-154\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\35493\\AppData\\Roaming\\Python\\Python312\\site-packages\\nltk\\translate\\bleu_score.py:577: UserWarning: \n",
      "The hypothesis contains 0 counts of 3-gram overlaps.\n",
      "Therefore the BLEU score evaluates to 0, independently of\n",
      "how many N-gram overlaps of lower order it contains.\n",
      "Consider using lower n-gram order or use SmoothingFunction()\n",
      "  warnings.warn(_msg)\n",
      "C:\\Users\\35493\\AppData\\Roaming\\Python\\Python312\\site-packages\\nltk\\translate\\bleu_score.py:577: UserWarning: \n",
      "The hypothesis contains 0 counts of 4-gram overlaps.\n",
      "Therefore the BLEU score evaluates to 0, independently of\n",
      "how many N-gram overlaps of lower order it contains.\n",
      "Consider using lower n-gram order or use SmoothingFunction()\n",
      "  warnings.warn(_msg)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.75"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-10T00:55:09.102564Z",
     "start_time": "2025-03-10T00:55:09.098083Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('Cumulative 1-gram: %f' % sentence_bleu(reference, candidate, weights=(1, 0, 0, 0)))\n",
    "\n",
    "print('Cumulative 2-gram: %f' % sentence_bleu(reference, candidate, weights=(0.5, 0.5, 0, 0)))\n",
    "\n",
    "print('Cumulative 3-gram: %f' % sentence_bleu(reference, candidate, weights=(0.33, 0.33, 0.33, 0)))\n",
    "\n",
    "print('Cumulative 4-gram: %f' % sentence_bleu(reference, candidate, weights=(0.25, 0.25, 0.25, 0.25)))"
   ],
   "id": "930ee7d1ec344080",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cumulative 1-gram: 0.750000\n",
      "Cumulative 2-gram: 0.500000\n",
      "Cumulative 3-gram: 0.000000\n",
      "Cumulative 4-gram: 0.000000\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-10T00:55:15.670680Z",
     "start_time": "2025-03-10T00:55:15.667206Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#这是2-gram的计算方法\n",
    "import numpy as np\n",
    "\n",
    "np.exp(1 / 2 * np.log(0.75) + 1 / 2 * np.log(0.33))"
   ],
   "id": "52276f0ce159c264",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.49749371855331004"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-10T00:55:21.238120Z",
     "start_time": "2025-03-10T00:55:21.233181Z"
    }
   },
   "cell_type": "code",
   "source": [
    "reference = [[i for i in range(10)]]\n",
    "candidate = [0, 1, 2, 3, 4, 5, 6, 7, 5, 9]\n",
    "score = sentence_bleu(reference, candidate)\n",
    "score"
   ],
   "id": "5cc8039bd5bfd01b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7825422900366437"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "abb5e049e77ee55d"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
